#include "StdAfx.h"

IDirect3DDevice9* gDevice = NULL;
IDirect3D9* gD3d = NULL;

LRESULT __stdcall WndProc(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg) 
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWindow, uMsg, wParam, lParam);
}

void initDevice(HWND hWindow) {
	gD3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS params = { 0 };
	params.AutoDepthStencilFormat = D3DFMT_D24S8;
	params.BackBufferFormat = D3DFMT_X8R8G8B8;
	params.BackBufferHeight = 600;
	params.BackBufferWidth = 800;
	params.EnableAutoDepthStencil = true;
	params.hDeviceWindow = hWindow;
	params.SwapEffect = D3DSWAPEFFECT_DISCARD;
	params.Windowed = true;

	gD3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &params, &gDevice);
}

void renderFrame() {
	gDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);
	gDevice->BeginScene();

	gDevice->EndScene();
	gDevice->Present(NULL, NULL, NULL, NULL);
}

void destroyDevice() {
	gD3d->Release();
	gDevice->Release();
}

BOOL WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
	WNDCLASS cls = { 0 };
	cls.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
	cls.hCursor = LoadCursor(0, IDC_ARROW);
	cls.hIcon = LoadIcon(0, IDI_APPLICATION);
	cls.hInstance = GetModuleHandle(NULL);
	cls.lpszClassName = L"_Blinded_Class";
	cls.style = CS_VREDRAW | CS_HREDRAW;
	cls.lpfnWndProc = WndProc;

	RegisterClass(&cls);

	RECT rc = { 0, 0, 800, 600 };
	AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
	HWND hWindow = CreateWindow(L"_Blinded_Class", L"Blinded Fenster", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, GetModuleHandle(NULL), NULL);

	initDevice(hWindow);

	ShowWindow(hWindow, SW_SHOW);
	UpdateWindow(hWindow);

	MSG msg;
	bool running = true;
	HANDLE dummyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	while(running) {
		while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
			if(msg.message == WM_QUIT) {
				running = false;
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		renderFrame();
		WaitForSingleObject(dummyEvent, 10);
	}

	DestroyWindow(hWindow);
	UnregisterClass(L"_Blinded_Class", GetModuleHandle(NULL));

	destroyDevice();

	return TRUE;
}